package com.panshi.repast.Cache;

import android.graphics.Bitmap;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.panshi.repast.ImageCache.CacheMemUtil;
import com.panshi.repast.ImageCache.ImageLoader;
import com.panshi.repast.ImageCache.ImageLoader.ImageCallback;

/**
 * 获取网络图片 , 异步加载
 * 
 * @author Administrator
 * 
 */
public class AdapterCache {
	public static Bitmap btm = null;

	/**
	 * 网络获取图片 ，Adapter
	 * 
	 * @param url
	 * @param image
	 * @param adapter
	 * @param imag
	 */
	public static void loadImage(final String url, final ImageView image,
			final BaseAdapter adapter, Integer imag) {
		Bitmap bm = CacheMemUtil.getImageFromCache(url); // 第一步，仅仅只从内存中读取
		if (bm != null) {
			btm = bm;
			image.setImageBitmap(bm);
		} else {
			image.setImageResource(imag);// 默认图片
			ImageLoader.getInstance().loadBitmap(url, new ImageCallback() {

				public void imageLoaded(Bitmap bm) {
					if (bm != null) {
						CacheMemUtil.putImage2Cache(url, bm);
						adapter.notifyDataSetChanged();
						btm = bm;
						// image.setImageBitmap(bm); //不能使用这句方法，否则会在加载完之前错误显示

					}
				}
			});
		}
	}

	/**
	 * 网络获取图片，单张
	 * 
	 * @param url
	 * @param image
	 * @param imag
	 */
	public static void loadImage(final String url, final ImageView image,
			Integer imag) {
		Bitmap bm = CacheMemUtil.getImageFromCache(url); // 第一步，仅仅只从内存中读取
		if (bm != null) {
			image.setImageBitmap(bm);
		} else {
			image.setImageResource(imag);// 默认图片
			ImageLoader.getInstance().loadBitmap(url, new ImageCallback() {

				public void imageLoaded(Bitmap bm) {
					if (bm != null) {
						CacheMemUtil.putImage2Cache(url, bm);
						image.setImageBitmap(bm);
					}
				}
			});
		}
	}

}
